package com.orange.quickback.system.service.user.manage.impl;


import com.orange.quickback.system.api.common.result.QBootResult;
import com.orange.quickback.system.dao.user.manage.QBootRoleMenuRepository;
import com.orange.quickback.system.dao.user.manage.QBootRoleRepository;
import com.orange.quickback.system.entities.user.manage.QBootRole;
import com.orange.quickback.system.entities.user.manage.QBootRoleMenu;
import com.orange.quickback.system.service.user.manage.QBootRolesMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class QBootRolesMenuServiceImpl implements QBootRolesMenuService {
    @Resource
    QBootRoleMenuRepository qBootRoleMenuRepository;
    @Resource
    QBootRoleRepository qBootRoleRepository;
    @Override
    public QBootResult addNewPattern(List<String> roles , String pattern) {
        QBootRoleMenu qBootRoleMenu = new QBootRoleMenu();
        List<QBootRole> qbrList = new ArrayList<>();
        try {
            for (String role:roles){
                QBootRole qBootRole = qBootRoleRepository.findByRoleName(role);
                if(qBootRole==null)
                    throw new Exception("找不到对象");
                qbrList.add(qBootRole);
            };
            qBootRoleMenu.setPattern(pattern);
            qBootRoleMenu.setQBootRoles(qbrList);
            qBootRoleMenuRepository.save(qBootRoleMenu);
            return QBootResult.build("增加成功");
        }catch (Exception e) {
            return QBootResult.buildFail(e.getMessage());
        }
    }

}
